<template>
  <CustomContent>
    <template v-slot:body>

      <el-collapse v-model="activeNames">
        <el-collapse-item name="1" class="app-car-container">
          <MmvTitle slot="title" title="租金"></MmvTitle>
          <el-row>
            <div class="item-title">
              费用信息
              <el-popover placement="right" trigger="click">
                <div class="ant-popover-inner-content">
                  <div class="fee-detail-wrapper">
                    <div class="day-item" v-for="(item,index) in rentDetail.orderPriceCalendars" :key="index">
                      <div class="date-weekname">
                        <div>{{ item.sameDay }}</div>
                        <div>{{ item.dateOfWeek|dateOfWeek }}</div>
                      </div>
                      <div class="price-color word-break">¥{{ item.price }}</div>
                    </div>
                  </div>
                </div>
                <el-button slot="reference" icon="el-icon-search" type="text">日租金明细</el-button>
              </el-popover>
            </div>
            <el-table :data="rentDetail.rent_items" :span-method="objectSpanMethod" border>
              <el-table-column label="" align="center" prop="name">
                <template slot-scope="{ row }">
                  {{ row.name }}
                  <template v-if="row.remark">
                    <el-button type="text" v-if="row.remark == 'renewal'" @click="showRecord()"><i class="el-icon-warning-outline"></i></el-button>
                    <el-button type="text" v-else-if="row.remark == 'usedMoney'" @click="showUsedMoney=true"><i class="el-icon-warning-outline"></i></el-button>
                    <el-tooltip placement="right-start" effect="dark" v-else>
                      <div slot="content" style="font-size: 14px">
                        {{ row.remark }}
                      </div>
                      <el-button type="text"><i class="el-icon-warning-outline"></i></el-button>
                    </el-tooltip>
                  </template>


                </template>
              </el-table-column>
              <el-table-column label="收费项名称" align="center" prop="chargeProjectName"/>
              <el-table-column label="单价" align="center" prop="unitPrice"/>
              <el-table-column label="收费类型" align="center" prop="chargeItemsType">
                <template slot-scope="{ row }">
                  <dict-tag :options="dict.type.charge_type" :value="row.chargeItemsType"/>
                </template>
              </el-table-column>
              <el-table-column label="数目" align="center" prop="number"/>
              <el-table-column label="费用" align="center" prop="chargeMoney"/>
            </el-table>
          </el-row>

          <el-row v-if="rentDetail.rent_moneyRemark" style="margin-top: 10px;">
            <el-descriptions :column="1" border>
              <el-descriptions-item label-class-name="my-descriptions-label" :labelStyle="{width:'200px'}">
                <template slot="label"> 费用说明:</template>
                {{ rentDetail.rent_moneyRemark }}
              </el-descriptions-item>
            </el-descriptions>
          </el-row>
          <el-row v-if="rentDetail.rent_payRecords.length>0">
            <div class="item-title-box">
              <span class="item-title">收款明细</span>
            </div>
            <OrderPayment :chargingItems="rentDetail.rent_payRecords"></OrderPayment>
          </el-row>
          <el-row v-if="rentDetail.rent_refundDetails.length>0">
            <div class="item-title">退款明细</div>
            <OrderPayment :chargingItems="rentDetail.rent_refundDetails" :type="2"></OrderPayment>
          </el-row>
        </el-collapse-item>
        <el-collapse-item name="2" class="app-car-container">
          <MmvTitle slot="title" title="其他费用收入"></MmvTitle>
          <el-row>
            <el-table :data="otaOtherCostDetail.receivableList" border>
              <el-table-column label="费用项名称" align="center" prop="costName"/>
              <el-table-column label="支付金额" align="center" prop="payAmount"/>
              <el-table-column label="支付方式" align="center" prop="name">
                <template v-slot="{row}">
                  {{row.payWay|escrowPayway}}
                </template>
              </el-table-column>
              <el-table-column label="支付时间" align="center" prop="payTime"/>
              <el-table-column label="经办人" align="center" prop="agent"/>
              <el-table-column label="备注" align="center" prop="remark"/>
              <el-table-column label="支付凭证" align="center" prop="url">
                <template v-slot="{row}">
                  <el-image :key="item" :src="row.urlList[0] | mmvImageUrl" :preview-src-list="[$mmvImageUrl(row.urlList[0])]" class="mmv-image"
                            fit="contain"></el-image>
                </template>

              </el-table-column>
            </el-table>
          </el-row>
        </el-collapse-item>
        <el-collapse-item name="3" class="app-car-container">
          <MmvTitle slot="title" title="其他费用支出"></MmvTitle>
          <el-row>
            <el-table :data="otaOtherCostDetail.payableList" border>
              <el-table-column label="费用项名称" align="center" prop="costName"/>
              <el-table-column label="支付金额" align="center" prop="payAmount"/>
              <el-table-column label="支付方式" align="center" prop="name">
                <template v-slot="{row}">
                  {{row.payWay|escrowPayway}}
                </template>
              </el-table-column>
              <el-table-column label="支付时间" align="center" prop="payTime"/>
              <el-table-column label="经办人" align="center" prop="agent"/>
              <el-table-column label="备注" align="center" prop="remark"/>
              <el-table-column label="支付凭证" align="center" prop="url">
                <template v-slot="{row}">
                  <el-image v-if="row.url" :key="item" :src="row.url | mmvImageUrl" :preview-src-list="[$mmvImageUrl(row.url)]" class="mmv-image"
                            fit="contain"></el-image>
                </template>

              </el-table-column>
            </el-table>
          </el-row>
        </el-collapse-item>
        <el-collapse-item name="5" class="app-car-container">
          <MmvTitle slot="title" title="违章费用"></MmvTitle>
          <el-row>
            <div class="item-title">费用信息</div>
            <el-table :data="violationDetail.violation_items" border :span-method="objectSpanMethod3">
              <el-table-column label="违章单据号" align="center" prop="documentNo"/>
              <el-table-column label="罚款" align="center" prop="illegalFines"/>
              <el-table-column label="扣分" align="center" prop="pointsDeduction"/>
              <el-table-column label="违章手续费" align="center" prop="illegalPoundage"/>
              <el-table-column label="处理状态" align="center" prop="processingStatus">
                <template slot-scope="{row}">
                  {{ {1: '未处理', 2: '已处理未缴费', 3: '已处理', 4: '已缴费未处理'}[Number(row.processingStatus)] }}
                </template>
              </el-table-column>
              <el-table-column label="违章服务费" align="center" prop="deductions"/>
            </el-table>
          </el-row>

          <el-row v-if="violationDetail.violation_payRecords.length>0">
            <div class="item-title-box">
              <span class="item-title">收款明细</span>
              <span>{{ violationDetail.violation_moneyRemark }}</span>
            </div>
            <OrderPayment :chargingItems="violationDetail.violation_payRecords"></OrderPayment>
          </el-row>
        </el-collapse-item>
      </el-collapse>



      <el-dialog title="已使用费用" :visible.sync="showUsedMoney" width="1000px" destroy-on-close append-to-body>
        <el-table :data="usedMoney" border>
          <el-table-column label="收费项名称" align="center" prop="chargeProjectName" width="200px"/>
          <el-table-column label="单价" align="center" prop="unitPrice"/>
          <el-table-column label="收费类型" align="center" prop="chargeItemsType">
            <template slot-scope="{ row }">
              <dict-tag :options="dict.type.charge_type" :value="row.chargeItemsType"/>
            </template>
          </el-table-column>
          <el-table-column label="数目" align="center" prop="number"/>
          <el-table-column label="费用" align="center" prop="chargeMoney"/>
          <el-table-column label="已使用费用" align="center" prop="usedMoney">
            <template slot-scope="{ row }">
              {{ row.usedMoney ? row.usedMoney.toFixed(2) : '0' }}
            </template>
          </el-table-column>
        </el-table>
        <el-descriptions :column="1" border :labelStyle="{width:'200px',background:'#fff',color:'#606266',textAlign:'center'}" :contentStyle="{textAlign:'center'}">
          <el-descriptions-item label="优惠" v-if="usedMoney_preferential">{{ usedMoney_preferential.toFixed(2) }}</el-descriptions-item>
          <el-descriptions-item label="总费用">{{ usedMoney_total ? usedMoney_total.toFixed(2) : '0' }}</el-descriptions-item>
          <el-descriptions-item label="已使用费用总计">{{ usedMoney_used ? usedMoney_used.toFixed(2) : '0' }}</el-descriptions-item>
        </el-descriptions>
      </el-dialog>
    </template>

  </CustomContent>
</template>

<script>
import MmvTitle from '@/components/MmvUI/MmvTitle.vue'
import * as api from "@/api/order";
import CustomContent from "@/components/custom/content";
import OrderPayment from "@/views/order/orderBasic/components/orderPayment";
import {getSupplementaryPaymentList} from "@/api/order";
import {escrowPayway} from "@/utils/filters";

export default {
  components: {
    CustomContent,
    MmvTitle,
    OrderPayment

  },
  dicts: ['charge_type', 'sys_pay_way'],
  data() {
    return {
      activeNames: ['1', '2', '3', '4', '5'],
      //费用信息
      rentDetail: {
        orderStatus: 0,
        rent_items: [],
        rent_moneyRemark: '',
        rent_payRecords: [],
        rent_refundDetails: [],
        orderPriceCalendars: [],
      },
      depositDetail: {
        deposit_items: [],
        deposit_payRecords: [],
        deposit_refundDetails: [],
      },
      illegalDetail: {
        illegal_items: [],
        illegal_payRecords: [],
        illegal_refundDetails: [],
      },
      vehicleDetail: {
        vehicle_items: [],
        vehicle_payRecords: []
      },
      violationDetail: {
        violation_items: [],
        violation_payRecords: []
      },
      otaOtherCostDetail:{
        receivableList:[],
        payableList:[]
      },
      orderNo: null,
      renewRecord: [],
      showRecordDetail: false,
      usedMoney: [],
      usedMoney_total: 0,
      usedMoney_preferential: 0,
      usedMoney_used: 0,
      showUsedMoney: false,
      loading: true
    }
  },
  filters: {
    dateOfWeek(val) {
      return {
        1: '星期一',
        2: '星期二',
        3: '星期三',
        4: '星期四',
        5: '星期五',
        6: '星期六',
        7: '星期日',
      }[Number(val)] || '';
    },
  },
  mounted() {
    this.orderNo = this.$route.query.orderNo
    this.costDetail()
  },
  methods: {
    escrowPayway,
    changgeorderNo(orderNo) {
      this.orderNo = orderNo;
      this.costDetail()
    },
    costDetail() {
      api.orderRentDetail(this.orderNo).then((response) => {
        this.rentDetail = {
          orderStatus: response.data.orderStatus || 0,
          rent_items: response.data.rent_items || [],
          rent_moneyRemark: response.data.rent_moneyRemark || '',
          rent_payRecords: response.data.rent_payRecords || [],
          rent_refundDetails: response.data.rent_refundDetails || [],
          orderPriceCalendars: response.data.orderPriceCalendars || [],
        }
        this.usedMoney = response.data.usedMoney
        this.usedMoney_total = response.data.usedMoney_total
        this.usedMoney_preferential = response.data.usedMoney_preferential
        this.usedMoney_used = response.data.usedMoney_used
        this.loading = false
      })
      // api.orderDepositDetail(this.orderNo).then((response) => {
      //   this.depositDetail = {
      //     deposit_items: response.data.deposit_items || [],
      //     deposit_payRecords: response.data.deposit_payRecords || [],
      //     deposit_refundDetails: response.data.deposit_refundDetails || [],
      //     deposit_moneyRemark: response.data.deposit_moneyRemark || '',
      //   }
      //   this.loading = false
      // })
      // api.orderIllegalDetail(this.orderNo).then((response) => {
      //   this.illegalDetail = {
      //     illegal_items: response.data.illegal_items || [],
      //     illegal_payRecords: response.data.illegal_payRecords || [],
      //     illegal_refundDetails: response.data.illegal_refundDetails || [],
      //     illegal_moneyRemark: response.data.illegal_moneyRemark || '',
      //   }
      //   this.loading = false
      // })
      // api.orderVehicleDetail(this.orderNo).then((response) => {
      //   this.vehicleDetail = {
      //     vehicle_items: response.data.vehicle_items || [],
      //     vehicle_payRecords: response.data.vehicle_payRecords || [],
      //     vehicle_moneyRemark: response.data.vehicle_moneyRemark || ''
      //   }
      //   this.loading = false
      // })
      api.orderViolationDetail(this.orderNo).then((response) => {
        this.violationDetail = {
          violation_items: response.data.violation_items || [],
          violation_payRecords: response.data.violation_payRecords || [],
          violation_moneyRemark: response.data.violation_moneyRemark || ''
        }
        this.loading = false
      })

      api.getSupplementaryPaymentList(this.orderNo).then((response) => {
        this.otaOtherCostDetail = {
          receivableList: response.data.receivableList || [],
          payableList: response.data.payableList || [],
        }
        this.loading = false
      })

    },
    showRecord() {
      this.showRecordDetail = false
      api.listRenewRecord({orderNo: this.orderNo}).then((response) => {
        this.renewRecord = response.data
        this.showRecordDetail = true
      })
    },
    objectSpanMethod({row, column, rowIndex, columnIndex}) {
      let rowspan = this.rentDetail.rent_items.filter(item => {
        return item.type == 1
      }).length
      if (columnIndex === 0) {
        if (rowIndex === 0 && rowIndex < rowspan) {
          return {
            rowspan: rowspan,
            colspan: 1,
          }
        } else if (rowIndex > 0 && rowIndex < rowspan) {
          return {
            rowspan: 0,
            colspan: 0,
          }
        }
      }
      if (rowIndex >= rowspan) {
        if (columnIndex === 0) {
          return [1, 1]
        } else if (columnIndex === 5) {
          return [1, 5]
        } else if (columnIndex < 6) {
          return [0, 0]
        }
      }
    },
    objectSpanMethod3({row, column, rowIndex, columnIndex}) {
      let rowspan = this.violationDetail.violation_items.length - 4
      if (rowIndex >= rowspan) {
        if (columnIndex === 0) {
          return [1, 1]
        } else if (columnIndex === 5) {
          return [1, 5]
        } else if (columnIndex < 6) {
          return [0, 0]
        }
      }
    },
  }
}
</script>
<style scoped>
.item-title-box {
  margin: 10px 0;
  color: #515a6e;
}

.item-title {
  margin: 10px;
  color: #000000;
}

.fee-detail-wrapper {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  max-width: 420px;
  -webkit-box-orient: horizontal;
  -webkit-box-direction: normal;
  -ms-flex-direction: row;
  flex-direction: row;
  -ms-flex-wrap: wrap;
  flex-wrap: wrap;
  -webkit-box-pack: start;
  -ms-flex-pack: start;
  justify-content: flex-start;
  max-height: 400px;
  overflow-y: auto;
}

.day-item {
  width: 80px;
  text-align: center;
  margin-top: 10px;
}

.price-color {
  color: #ff8b26;
}
</style>

